﻿using BaseUi.ViewModel;
using Ping9719.WpfEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseUi
{
    /// <summary>
    /// 设备监控
    /// </summary>
    public class ExcelDeviceReadModel
    {
        public string 名称 { get; set; }
        public string 组名 { get; set; }
        public string 类型 { get; set; }
        public string 地址 { get; set; }
        public string 单位 { get; set; }

        public static List<DeviceReadModel> GetDatas(string excelPath, string sheetName = "设备监控")
        {
            return MiniExcelLibs.MiniExcel.Query<ExcelDeviceReadModel>(excelPath, sheetName)
                .Where(o => !string.IsNullOrWhiteSpace(o.名称))
                .Select(o => new DeviceReadModel()
                {
                    Name = o.名称,
                    ExcelTag = o,
                }).ToList();
        }
    }

    /// <summary>
    /// 设备控制
    /// </summary>
    public class ExcelDeviceWriteModel
    {
        public string 名称 { get; set; }
        public string 组名 { get; set; }
        public string 类型 { get; set; }
        public string 读地址 { get; set; }
        public string 写地址 { get; set; }
        public string 点动切换 { get; set; }
        public string 单位 { get; set; }

        public static List<DeviceWriteModel> GetDatas(string excelPath, string sheetName = "设备控制")
        {
            return MiniExcelLibs.MiniExcel.Query<ExcelDeviceWriteModel>(excelPath, sheetName)
                .Where(o => !string.IsNullOrWhiteSpace(o.名称))
                .Select(o => new DeviceWriteModel()
                {
                    Name = o.名称,
                    ExcelTag = o,
                }).ToList();
        }
    }

    /// <summary>
    /// 设备气缸
    /// </summary>
    public class ExcelDeviceUrnModel
    {
        public string 名称 { get; set; }
        public string 组名 { get; set; }
        public string 推地址 { get; set; }
        public string 回地址 { get; set; }
        public string 推到位地址 { get; set; }
        public string 回到位地址 { get; set; }
        public string 点动切换 { get; set; }

        public static List<DeviceUrnModel> GetDatas(string excelPath, string sheetName = "设备气缸")
        {
            return MiniExcelLibs.MiniExcel.Query<ExcelDeviceUrnModel>(excelPath, sheetName)
                .Where(o => !string.IsNullOrWhiteSpace(o.名称))
                .Select(o => new DeviceUrnModel()
                {
                    Name = o.名称,
                    ExcelTag = o,
                }).ToList();
        }
    }

    /// <summary>
    /// 设备伺服
    /// </summary>
    public class ExcelDeviceServoModel
    {
        public string 名称 { get; set; }
        public string 组名 { get; set; }
        public string 手动速度获取 { get; set; }
        public string 手动速度设置 { get; set; }
        public string 自动速度获取 { get; set; }
        public string 自动速度设置 { get; set; }
        public string 当前位置获取 { get; set; }
        public string 位置点动加 { get; set; }
        public string 位置点动减 { get; set; }
        public string 位置移动 { get; set; }

        public static List<DeviceServoModel> GetDatas(string excelPath, string sheetName = "设备伺服")
        {
            return MiniExcelLibs.MiniExcel.Query<ExcelDeviceServoModel>(excelPath, sheetName)
                .Where(o => !string.IsNullOrWhiteSpace(o.名称))
                .Select(o => new DeviceServoModel()
                {
                    Name = o.名称,
                    ExcelTag = o,
                }).ToList();
        }
    }

    /// <summary>
    /// 设备报警
    /// </summary>
    public class ExcelDeviceAlarmModel
    {
        public string 名称 { get; set; }
        public string 地址 { get; set; }
        public string 类型 { get; set; }
        public string 触发 { get; set; }
        public string 级别 { get; set; }
        public string 解决方式 { get; set; }

        public static List<ExcelDeviceAlarmModel> GetDatas(string excelPath, string sheetName = "设备报警")
        {
            return MiniExcelLibs.MiniExcel.Query<ExcelDeviceAlarmModel>(excelPath, sheetName).Where(o => !string.IsNullOrWhiteSpace(o.名称)).ToList();
        }

        /// <summary>
        /// 读取设备中的警报
        /// </summary>
        /// <param name="models">从excel中读取到的所有警告</param>
        /// <param name="plc">设备</param>
        public static List<ExcelDeviceAlarmModel> ReadPlcAlarm(List<ExcelDeviceAlarmModel> models, object plc)
        {
            if (plc == null)
                return new List<ExcelDeviceAlarmModel>();

            List<ExcelDeviceAlarmModel> excelErr = new List<ExcelDeviceAlarmModel>();
            //var plc1 = (Ping9719.DeviceCommunication.Interfaces.IIoTBase)plc;
            //foreach (var item in models)
            //{
            //    object? val = null;
            //    if (item.类型.ToLower() == "bool")
            //    {
            //        var plcInfo = plc1.Read<bool>(item.地址);
            //        val = plcInfo.IsSucceed ? plcInfo.Value : null;
            //    }
            //    else if (item.类型.ToLower() == "int")
            //    {
            //        var plcInfo = plc1.Read<Int16>(item.地址);
            //        val = plcInfo.IsSucceed ? plcInfo.Value : null;
            //    }

            //    if (item.IsOnAlarm(val))
            //        excelErr.Add(item);
            //}
            return excelErr;
        }

        /// <summary>
        /// 是否触发警报
        /// </summary>
        public bool IsOnAlarm(object? val)
        {
            try
            {
                if (val == null)
                    return false;

                string vs = val.ToString().ToLower();
                if (类型.Trim().ToLower().Contains("bool"))
                {
                    if (vs == 触发.Trim().ToLower())
                        return true;
                    else
                        return false;
                }
                else if (类型.Trim().ToLower().Contains("int"))
                {
                    if (string.IsNullOrWhiteSpace(触发))
                        return false;
                    if (!触发.Contains(vs))
                        return false;

                    foreach (var item in 触发.Split(';'))
                    {
                        var kv = item.Split(':');
                        if (kv.Length != 2)
                            continue;

                        if (kv[0] == vs)
                            return true;
                    }
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }

    public class IotDevice
    {
        public static Action<IotDevice> UserChange;

        /// <summary>
        /// 控件名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 控件类型
        /// </summary>
        public string Type { get; set; }
        /// <summary>
        /// 控件值
        /// </summary>
        public object Val { get; set; }
        /// <summary>
        /// 功能
        /// </summary>
        public string Funct { get; set; }
        /// <summary>
        /// 操作方式（点动、切换）
        /// </summary>
        public string Mode { get; set; }
    }
}
